package com.wxq.workpad.lailiao.adapter

import android.app.DatePickerDialog
import android.app.DatePickerDialog.OnDateSetListener
import android.content.Context
import android.graphics.Color
import android.graphics.drawable.ColorDrawable
import android.text.Editable
import android.text.TextWatcher
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.*
import androidx.recyclerview.widget.RecyclerView
import com.wxq.workpad.R
import com.wxq.workpad.entity.SaiXuanItem

/**
 * @Description: 类作用描述
 */
class SaiXuanListAdapter(context: Context):RecyclerView.Adapter<RecyclerView.ViewHolder>() {
    private var mContext:Context

    private var mData:MutableList<SaiXuanItem>

    private lateinit var listener:(position:SaiXuanItem)->Unit

    init {
        mContext=context
        mData= mutableListOf()
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
        return ItemHold(LayoutInflater.from(mContext).inflate(R.layout.item_saixuan_with_input,parent,false))
    }

    override fun getItemCount(): Int {
        if (mData==null){
            return 0;
        }
        return mData.size
    }

    fun updateDate(data:MutableList<SaiXuanItem>){
         mData=data
         notifyDataSetChanged()
    }

    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
       // 数量少不缓存 防止edittext 异常
        holder.setIsRecyclable(false);
        if (holder is ItemHold){
            val item = mData.get(position)
            holder.rl_type1.visibility=View.GONE
            holder.rl_type2.visibility=View.GONE
            holder.rl_type3.visibility=View.GONE



            if (item.type==1) { // 模糊查询
                holder.rl_type1.visibility=View.VISIBLE
                holder.tv_tab1.setText(item.name+":")

                holder.et_tab1.setText(item.value)
                holder.et_tab1.setHint("请输入"+item.name)
                holder.et_tab1.addTextChangedListener (object :TextWatcher{
                    override fun beforeTextChanged(
                        s: CharSequence?,
                        start: Int,
                        count: Int,
                        after: Int
                    ) {

                    }

                    override fun onTextChanged(
                        s: CharSequence?,
                        start: Int,
                        before: Int,
                        count: Int
                    ) {

                    }

                    override fun afterTextChanged(s: Editable?) {
                        item.value=s.toString()
                    }

                })

                holder.iv_tab1_delete.setOnClickListener{
                    // 移除 这一项
                     item.value=""
                     item.isSelect=false
                    // 移除回调
                    if (listener!=null) {
                        listener(item)
                    }


                      mData.removeAt(position)
                    notifyDataSetChanged()

                }



            }

            if (item.type==2) { // 单选
                holder.rl_type2.visibility=View.VISIBLE

                holder.tv_tab2.setText(
                    item.name+":"
                )
                holder.iv_tab2_delete.setOnClickListener{
                    // 移除 这一项
                    item.value=""
                    item.isSelect=false
                    // 移除回调
                    if (listener!=null) {
                        listener(item)
                    }


                    mData.removeAt(position)
                    notifyDataSetChanged()

                }


                holder.tv_radio_left.setText(item.chooseItemLeft)
                holder.tv_radio_right.setText(item.chooseItemRight)

                holder.ll_radio_left.setOnClickListener{
                     holder.iv_radio_left.setBackgroundResource(R.mipmap.radiobuttonselect)
                     holder.iv_radio_right.setBackgroundResource(R.mipmap.radiobuttonunselect)
                    item.value=item.chooseItemLeft
                }

                holder.ll_radio_right.setOnClickListener{
                    holder.iv_radio_left.setBackgroundResource(R.mipmap.radiobuttonunselect)
                    holder.iv_radio_right.setBackgroundResource(R.mipmap.radiobuttonselect)
                    item.value=item.chooseItemRight
                }


                if (item.value.equals(item.chooseItemRight)) {
                    holder.iv_radio_left.setBackgroundResource(R.mipmap.radiobuttonunselect)
                    holder.iv_radio_right.setBackgroundResource(R.mipmap.radiobuttonselect)
                }else if (item.value.equals(item.chooseItemLeft)){
                    holder.iv_radio_left.setBackgroundResource(R.mipmap.radiobuttonselect)
                    holder.iv_radio_right.setBackgroundResource(R.mipmap.radiobuttonunselect)
                }else{
                    holder.iv_radio_left.setBackgroundResource(R.mipmap.radiobuttonunselect)
                    holder.iv_radio_right.setBackgroundResource(R.mipmap.radiobuttonunselect)
                }

            }

            if (item.type==3) { // 日期
                holder.rl_type3.visibility=View.VISIBLE
                holder.tv_start_time.setOnClickListener {

                    showDatePickerDetailDialog(object :DatePickerDialog.OnDateSetListener{
                        override fun onDateSet(
                            view: DatePicker?,
                            year: Int,
                            month: Int,
                            dayOfMonth: Int
                        ) {
                            holder.tv_start_time.setText(year.toString()+"/"+(month+1)+"/"+dayOfMonth)
                        }

                    },2023,0,1)

                }


                holder.tv_end_time.setOnClickListener {

                    showDatePickerDetailDialog(object :DatePickerDialog.OnDateSetListener{
                        override fun onDateSet(
                            view: DatePicker?,
                            year: Int,
                            month: Int,
                            dayOfMonth: Int
                        ) {
                            holder.tv_end_time.setText(year.toString()+"/"+(month+1)+"/"+dayOfMonth)
                        }

                    },2023,0,1)

                }


                holder.iv_tab3_delete.setOnClickListener{
                    // 移除 这一项
                    item.value=""
                    item.isSelect=false
                    // 移除回调
                    if (listener!=null) {
                        listener(item)
                    }


                    mData.removeAt(position)
                    notifyDataSetChanged()

                }


            }




        }
    }


    /**
     * 显示时间选择控件
     */
    private fun showDatePickerDetailDialog(
        listener: OnDateSetListener,
        year: Int,
        month: Int,
        day: Int
    ) {
        val datePickerDialog = DatePickerDialog(
            mContext,
            android.R.style.Theme_Holo_Light_Dialog,
            listener,
            year,
            month,
            day
        )
        if (datePickerDialog.window != null) {
            datePickerDialog.window!!.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
        }
        datePickerDialog.setOnDismissListener {

        }
        datePickerDialog.show()
    }


    fun setRemoveItemListener(itemClick:(position:SaiXuanItem)->Unit){
        listener=itemClick
    }


    inner class ItemHold(itemView: View) : RecyclerView.ViewHolder(itemView) {
        var tv_tab1:TextView
        var tv_tab2:TextView
        var et_tab1:EditText
        var iv_tab1_delete:ImageView
        var iv_tab2_delete:ImageView
        var iv_tab3_delete:ImageView
        var rl_type1:RelativeLayout
        var rl_type2:RelativeLayout
        var rl_type3:RelativeLayout
        var tv_start_time:TextView
        var tv_end_time:TextView


        //
        var ll_radio_left:LinearLayout
        var ll_radio_right:LinearLayout

        var iv_radio_left:ImageView
        var iv_radio_right:ImageView
        var tv_radio_left:TextView
        var tv_radio_right:TextView

        init {
            tv_tab1= itemView.findViewById(R.id.tv_tab1)
            tv_tab2= itemView.findViewById(R.id.tv_tab2)
            et_tab1= itemView.findViewById(R.id.et_tab1)
            iv_tab1_delete= itemView.findViewById(R.id.iv_tab1_delete)
            iv_tab2_delete= itemView.findViewById(R.id.iv_tab2_delete)
            iv_tab3_delete= itemView.findViewById(R.id.iv_tab3_delete)
            rl_type1= itemView.findViewById(R.id.rl_type1)
            rl_type2= itemView.findViewById(R.id.rl_type2)
            rl_type3= itemView.findViewById(R.id.rl_type3)
            tv_start_time= itemView.findViewById(R.id.tv_start_time)
            tv_end_time= itemView.findViewById(R.id.tv_end_time)
            ll_radio_left= itemView.findViewById(R.id.ll_radio_left)
            ll_radio_right= itemView.findViewById(R.id.ll_radio_right)
            iv_radio_left= itemView.findViewById(R.id.iv_radio_left)
            iv_radio_right= itemView.findViewById(R.id.iv_radio_right)
            tv_radio_right= itemView.findViewById(R.id.tv_radio_right)
            tv_radio_left= itemView.findViewById(R.id.tv_radio_left)

        }
    }
}